import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/login/login_viewmodel.dart';
import 'package:provider/provider.dart';

//加载转状态
enum MyCountdownMode {
  login, //登录
  normal, //正常加载
}

//倒计时
class MyCountdownButton extends StatefulWidget {
  //倒计时
  final TextStyle? textStyle;
  final TextStyle? countingTextStyle;
  final TextEditingController? textController;
  final TextEditingController? codeController;
  final VoidCallback? onStart;
  final VoidCallback? onOver;
  final MyCountdownMode? mode;
  final int? countdown;
  final bool isStartDown;
  const MyCountdownButton(
      {super.key,
      this.textStyle,
      this.countingTextStyle,
      this.onStart,
      this.onOver,
      this.textController,
      this.codeController,
      this.mode = MyCountdownMode.login,
      this.countdown,
      required this.isStartDown});

  @override
  State<MyCountdownButton> createState() => _MyCountdownButtonState();
}

class _MyCountdownButtonState extends State<MyCountdownButton> {
  int _countdown = 60;
  bool _isCounting = false;

  @override
  void initState() {
    super.initState();
    _countdown = widget.countdown ?? 60;
  }

  void _startCountdown() async {
    setState(() {
      _isCounting = true;
    });

    while (_countdown > 0 && _isCounting) {
      await Future.delayed(const Duration(seconds: 1));
      setState(() {
        _countdown--;
      });
    }
    widget.onOver?.call();
    _isCounting = false;
   
  }

  @override
  Widget build(BuildContext context) {
    if (widget.isStartDown && !_isCounting) {
      _startCountdown();
    }
    TextStyle textStyle =
        widget.textStyle ?? TextStyle(color: Colors.white, fontSize: 13.sp, fontWeight: FontWeight.w700);
    TextStyle countingTextStyle =
        widget.countingTextStyle ?? TextStyle(color: Colors.white, fontSize: 13.sp, fontWeight: FontWeight.w700);

    return GestureDetector(
        onTap: () {
          //检测下
          if (widget.textController!.text.isEmpty) {
            EasyLoading.showToast("手机号不能为空");
            return;
          } else if (!CommonPlugin.isPhone(widget.textController!.text)) {
            EasyLoading.showToast("手机格式不正确");
            return;
          }

          if (widget.mode == MyCountdownMode.login) {
            LoginViewModel viewmodel = Provider.of<LoginViewModel>(context, listen: false);

            if (viewmodel.isShowGraphicVerificationCode) {
              //如果有图形验证码的情况下, 要验证下书否数据,
              if (widget.codeController!.text.isEmpty) {
                EasyLoading.showToast("图形验证码不能为空");
                return;
              }
            }

            if (!viewmodel.isAgreeProtocol) {
              // 没有选择用户协议, 也不让发送短信

              EasyLoading.showToast("请同意隐私政策");
              return;
            }
          }

          if (_isCounting) return;
          widget.onStart?.call();
        },
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 5.h), //#B0B0B0
          child: Text(
            _isCounting ? '($_countdown)重新发送' : '获取验证码',
            style: _isCounting ? countingTextStyle : textStyle,
          ),
        ));
  }
}
